<template>
  <div>
    <div style="text-align:left">
      患者信息：
      <el-tag>姓名：{{patient.realName}}</el-tag>
      <el-tag>病历号：{{patient.caseNumber}}</el-tag>
      <el-tag>年龄：{{patient.age}}</el-tag>
      <el-tag>性别：{{patient.gender}}</el-tag>
    </div>
    <el-divider></el-divider>
    <div style="font-size:20px;text-align:left">
      <i class="el-icon-document-checked">开设处方</i>
    </div>
    <el-divider></el-divider>
    <div style="display:flex">
      <el-button type="primary" disabled icon="el-icon-price-tag">处方金额：{{ total_price.toFixed(2) }}元</el-button>
    </div>
    <el-table :data="patient_drug_select_table" @selection-change="drugSelectionChange" ref="patient_drug_table">
      <el-table-column type="selection" width="30"></el-table-column>
      <el-table-column type="expand" width="55">
        <template #default="scope">
          <el-tag style="margin-left:110px">药品编码：{{scope.row.drugCode}}</el-tag>
          <el-tag style="margin-left:10px">包装单位：{{scope.row.drugUnit}}</el-tag>
          <el-tag style="margin-left:10px">生产厂家：{{scope.row.manufacturer}}</el-tag>
          <el-tag style="margin-left:10px">药剂类型：{{scope.row.drugDosage}}</el-tag>
          <el-tag style="margin-left:10px">药品类型：{{scope.row.drugType}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="药品名称" prop="drugName" width="280"></el-table-column>
      <el-table-column label="药品规格" prop="drugFormat"  width="160"></el-table-column>
      <el-table-column label="单价" prop="drugPrice" width="100"></el-table-column>
      <el-table-column label="用法用量">
        <template #default="scope">
          <el-input type="textarea"  :rows="1" v-model="scope.row.drugUsage" placeholder="输入用法用量，使用频次等信息"></el-input>
        </template>
      </el-table-column>
      <el-table-column label="数量" width="200">
        <template #default="scope">
          <el-input-number v-model="scope.row.drugNumber" :min="1" :max="100"
            :step="1" step-strictly @change="drugNumberChange"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column width="180">
        <template #header>
          <el-button type="text" @click="delDrugFromTable">删除</el-button>
          <el-button type="text" @click="drugDialogVisibale=true">增加</el-button>
        </template>
      </el-table-column>

    </el-table>
    <div style="text-align:left;margin-top:20px">
      <el-button type="primary" @click="addDrugPrescription">开立处方</el-button>
      <el-button type="primary" @click="resetDrugInput">重置处方</el-button>
    </div>

    <!-- 药品选择对话框 -->
    <el-dialog title="添加药品" v-model="drugDialogVisibale">
      <div>
        <el-tag style="width:20%">药品名称:</el-tag><el-input style="width:80%" v-model="drug_search.drugName" placeholder="请输入药品名称（支持模糊查询）"></el-input>
      </div>
      <div>
        <el-tag style="width:20%">拼音助记码:</el-tag><el-input style="width:80%" v-model="drug_search.drugCode" placeholder="请输入药品拼音助记码（支持模糊查询）"></el-input>
      </div>
      <div style="text-align:right">
        <el-button @click="searchDrug">搜索</el-button>
      </div>
      <el-table :data="view_search_drug_table" @selection-change="drugSelectionChange" ref="dialog_check_table">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="药品编码" prop="drugCode" width="150"></el-table-column>
        <el-table-column label="药品名称" prop="drugName"></el-table-column>
        <el-table-column label="药品规格" prop="drugFormat" width="100"></el-table-column>
        <el-table-column label="药品单价" prop="drugPrice" width="100"></el-table-column>
        <el-table-column label="包装单位" prop="drugUnit" width="100"></el-table-column>
        <el-table-column label="生产厂家" prop="manufacturer" width="150"></el-table-column>
      </el-table>
      <div>
        <el-button type="primary" @click="addSelectionDrug">添加</el-button>
        <el-button type="info" @click="closeDialog">关闭</el-button>
      </div>
    </el-dialog>

  </div>
</template>
<script>
import qs from 'qs'
import {isNullMessage} from "../../../util/util"
import {getSessionStorage} from "@/util/common";
import {ElMessage} from "element-plus";
export default {
  data(){
    return{
      submit_prescription:[],//提交的处方数据
      tempDrug:[],//对话框——临时复选框选中的数据
      drugDialogVisibale:false,//药品选择对话框是否显示
      patient:{}, //患者信息
      total_price:0,//处方金额合计
      patient_drug_select_table:[],//处方药品列表
      drug_search:{
        drugName:'',
        drugCode:''
      },//药品搜索数据
      view_search_drug_table:[],//显示药品搜索结果
    }    
  },
  methods:{

    //对话框添加
    addSelectionDrug:function (){
      this.drugDialogVisibale=false
      //显示对应选中的药品信息
      //数组全=合
      let newAarray=this.patient_drug_select_table.concat(this.tempDrug)
      //去重
      this.patient_drug_select_table=Array.from(new Set(newAarray))
      //所有药的数量默认为1
      this.patient_drug_select_table.forEach(item=>{
        item.drugNumber=1
      })
      // 计算总金额
      this.getCalTotal()
      //清除复选框选中
      this.$refs.dialog_check_table.clearSelection()
      console.log("--------------")
      console.log(this.patient_drug_select_table)
      console.log("--------------")
    },
    //对话框 关闭
    closeDialog:function(){
      this.drugDialogVisibale=false
    },
    //对话框选中时事件对应的函数
    drugSelectionChange:function (rows){
      //console.log(rows)
      this.tempDrug=rows
    },
    //对话框药品查询
    searchDrug:function (){

      let url='http://localhost:8092/prescription/selectdrug'
      this.$axios.post(url,this.drug_search).then(resp=>{
        this.view_search_drug_table=resp.data.result;

      })
    },

    //药品的数量改变时事件对应的函数
    drugNumberChange:function (){
      this.getCalTotal()
    },
    //计算总金额
    getCalTotal:function (){
      this.total_price=0
      this.patient_drug_select_table.forEach(item=>{

        this.total_price+=parseFloat(item.drugPrice)*parseInt(item.drugNumber)
      })
    },
    //删除选中的药品
    delDrugFromTable:function (){
      this.tempDrug.forEach(item=>{
        let index=this.$refs.patient_drug_table.data.indexOf(item)
        this.$refs.patient_drug_table.data.splice(index,1)
      })
      // 计算总金额
      this.getCalTotal()
    },
    //开处方事件对应的函数
    addDrugPrescription:function () {
      let url='http://localhost:8092/prescription/insert'
      this.patient_drug_select_table.forEach(item=>{
        item.registerId=this.patient.id
        item.drugState='已开立'
        item.drugId=item.id
      })
      this.$axios.post(url,this.patient_drug_select_table).then(resp=>{
       ElMessage({
         type:'success',
         message:'开处方成功！'
       })
        this.patient_drug_select_table=[]
        this.total_price=0.00
      })
    },
    //处方重置
    resetDrugInput:function () {
      this.patient_drug_select_table=[]
      this.total_price=0.00
    },
  },
  mounted() {
    if(getSessionStorage("patient")==null)
    {
      ElMessage({
        type:"warning",
        message:'请选择患者！'
      })
      return
    }
    this.patient=getSessionStorage("patient")
  }
}
</script>